#include "task_model.h"

static TASK_UINT TASK_Heartbeat = 0;
extern struct TaskModel_t *task_list[];

struct TaskModel_t **TASK_GET_LIST(TASK_V){
	return task_list;
}

TASK_UINT TASK_GET_Heartbeat(TASK_V){
	return TASK_Heartbeat;
}

TASK_V TASK_SET_DELAY(struct TaskModel_t *vtask, TASK_UINT delay){
	if(delay < 30000)
		vtask->Task_Delay = delay;
	else
		vtask->Task_Delay = 30000;
}

TASK_V TASK_ALL_INIT(struct TaskModel_t *gTask[]){

	printf("TASK_ALL_INIT \n");
	TASK_INT index = -1;
	TASK_INT i = TASK_SUM;
	
	struct TaskModel_t *task_model;

	while(i--){
		index++;
		task_model = gTask[index];
		if(task_model != NULL){
			task_model->state = task_model->TaskInit(gTask[index]);
		}else{
			printf("TASK_INIT_END\n");
			break;
		}
	}
}

TASK_V TASK_RUN_MACHINE(struct TaskModel_t *gTask[]){

	TASK_INT index = 0;
	TASK_INT task_sum = USER_TASK_SUM;
	struct TaskModel_t *vtask;

	TASK_INT task_polling = 0;
	for(task_polling = 0; task_polling < 601; task_polling++){	

		TASK_Heartbeat = (TASK_Heartbeat+1)%30000;
		
		vtask = gTask[0];
		vtask->state = vtask->TaskRun(gTask[0]);

		for(index = 0; index < task_sum; index++){//while(1)
			vtask = gTask[index+1];
			switch (vtask->state){
				case SSTATE_INITED:
					if(vtask != NULL)
						vtask->state = vtask->TaskInit(gTask[index+1]);
				break;
				case SSTATE_RUNNING:
					if(vtask != NULL)
						vtask->state = vtask->TaskRun(gTask[index+1]);
				break;
				case SSTATE_STOP:
					if(vtask != NULL)
						vtask->state = vtask->TaskWait(gTask[index+1]);
				break;
				default:break;
			}
		}
		
	}

	
} 

